/**
 * 
 */
package com.apache.website.service.plugins;

import java.util.Date;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.DateUtils;
import com.apache.website.entity.Manuscript;
import com.apache.website.manager.ManuscriptManager;

/**
 * @author dyhou
 *
 */
public class SaveManuscriptPluginImpl implements PluginConnector {

	/* (non-Javadoc)
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
	 */
	public Object execute(ParamsVo param) throws Exception {
		ParamsVo<Manuscript> vo = new ParamsVo<Manuscript>();
		Manuscript mt = new Manuscript();
		mt = (Manuscript) ClassToolsUtil.getInstance().convert2Bean(param.getParams(), mt);
		ResultEntity entity = new ResultEntity();
		if (Validator.isNull(mt.getManuscriptId())) {
			if (Validator.isNull(mt.getUrl())) {
				mt.setUrl(mt.getChannelCode() + "/" + DateUtils.format(new Date(), "yyyyMMdd") + "/"
						+ DateUtils.format(new Date(), "HHmmssSSS") + ".shtml");
			}
			mt.setStatus("0");
			mt.setViewNumber(0l);
			mt.setSeqNum(65535l);
			mt.setCreatedTime(DateUtils.Now.fmt_yyyyMMdd_HHmm());
			mt.setIsQuoted("T");
			mt.setIsPageBreak("F");
			vo.setObj(mt);
			vo.setParams("content", param.getParams("content"));
			String id = manuscriptManager.saveInfo(vo);
			entity.setResult("true");
			entity.setEntity(id);
			entity.setMessage("稿件信息保存成功");
			return entity;
		} else {
			if (Validator.isNull(mt.getUrl()) && "1".equals(mt.getType())) {
				mt.setUrl(mt.getChannelCode() + "/" + DateUtils.format(new Date(), "yyyyMMdd") + "/"
						+ DateUtils.format(new Date(), "HHmmssSSS") + ".shtml");
			}
			vo.setObj(mt);
			vo.setParams("content", param.getParams("content"));
			boolean mark = manuscriptManager.editInfo(vo);
			if (mark) {
				entity.setEntity("true");
				entity.setMessage("稿件信息修改成功");
				return entity;
			}
		}
		entity.setMessage("操作失败");
		return entity;
	}

	private ManuscriptManager manuscriptManager;

	public void setManuscriptManager(ManuscriptManager manuscriptManager) {
		this.manuscriptManager = manuscriptManager;
	}

}
